<template>
    <div class="login">

        <div class="login-all">

            <div class="login-header">
                <h3>登录到Chen</h3>
            </div>
            <div class="login-form">
                <el-form label-position="right" label-width="auto" :model="loginForm" style="max-width: 600px" @keyup.enter="loginSubmit">
                    <el-form-item label="用户名" label-position="right">
                        <el-input v-model="loginForm.username" />
                    </el-form-item>
                    <el-form-item label="密码" label-position="right">
                        <el-input v-model="loginForm.password" type="password" />
                    </el-form-item>
                </el-form>
            </div>
            <div class="submit">
                <el-button @click="loginSubmit">登录</el-button>
            </div>
        </div>
    </div>
</template>

<script setup>
import loginApi from "../api/login.js";
import { useRouter } from "vue-router";
import { reactive, ref, onMounted } from 'vue';
import { useUserStore } from "../stores/user.js";

const router = useRouter();
const userStore = useUserStore();

const loginForm = reactive({
    username: '',
    password: '',
})


const loginSubmit = async () => {
    const data = await loginApi.getToken(loginForm);
    if (data.code === 0) {
        userStore.state.token = data.data.account;
        localStorage.setItem("token", data.data.account);
        localStorage.setItem("refresh", data.data.refresh);
        // await getUserInfo(data.data.account);
        await userStore.restoreUser()
        router.push("/");
        return
    }
}

onMounted(() => {
    if (userStore.state.token) {
        router.push("/");
    }
})
</script>

<style lang="less" scoped>
.login {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: var(--main-bg);
    color: var(--text-primary);
    transition: background-color 0.3s, color 0.3s;
}

.login-all {
    background: #fff;
    color: #222;
    border-radius: 12px;
    box-shadow: 0 4px 24px rgba(0,0,0,0.12);
    padding: 32px 36px 24px 36px;
    min-width: 320px;
    max-width: 380px;
    width: 100%;
    transition: background 0.3s, color 0.3s;
}

.dark-theme .login-all {
    background: var(--card-bg, #232323);
    color: var(--text-primary, #eee);
    box-shadow: 0 4px 24px rgba(0,0,0,0.45);
}

.login-header {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 24px;
}

.login-form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: stretch;
    margin-bottom: 18px;
}

:deep(.el-form-item__label) {
    color: #888;
}
.dark-theme :deep(.el-form-item__label) {
    color: #bbb;
}

:deep(.el-input__wrapper) {
    background: #f7f7f7;
}
.dark-theme :deep(.el-input__wrapper) {
    background: #222 !important;
    color: #eee !important;
    border: 1px solid #444;
}

:deep(.el-input__inner) {
    color: #222;
}
.dark-theme :deep(.el-input__inner) {
    color: #eee;
}

.submit {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 8px;
}

:deep(.el-button) {
    min-width: 90px;
}
</style>